﻿using AutoMapper;
using FI.EntityFramework;
using FI.Security;
using Sample.BusinessEntities;
using Sample.Common;
using Sample.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Sample.DataAccess
{
    public class SitemapDA : Repository<Sitemap, ApplicationContext>
    {
        public SitemapDA()
            : base(new ApplicationContext())
        {

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Sitemap, SitemapModel>().ReverseMap();

            });

        }

        public IEnumerable<int> GetIDs()
        {
            var query = this.DbContext.People.Select(n => n.Id).ToList();

            return query;
        }

        public SitemapModel GetSitemap(int id)
        {
            return this.DbContext.People.Where(n => n.Id == id).ProjectToSingle<SitemapModel>();
        }

        public IEnumerable<SitemapModel> GetSitemapsByCriteria(string name, DateTime? birthdate, int page, out int totalCount)
        {
            var query = this.DbContext.People.WhereIf(!string.IsNullOrEmpty(name), n => n.Name == name).WhereIf(birthdate.HasValue, n => n.BirthDate == birthdate);

            totalCount = query.Count();

            return query.GetPageableData(Constants.PageSize, page, "CREATED_TIME", true).Select(n => Mapper.Map<SitemapModel>(n)).ToList();

        }

        public IEnumerable<SitemapModel> GetSitemaps()
        {
            return this.DbContext.People.ProjectToList<SitemapModel>();
        }

        public void AddSitemap(SitemapModel personModel)
        {
            var person = Mapper.Map<Sitemap>(personModel);

            MarkAsAdded(person);

            DbContext.SaveChanges();
        }
        public void UpdateSitemap(SitemapModel personModel)
        {

            var person = Mapper.Map<Sitemap>(personModel);

            MarkAsModified(person);

            DbContext.SaveChanges();
        }
      
    }
}
